import os
import json
import datetime
from IPython.display import Markdown, display


def load_json_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return json.load(file)


def add_record_to_json(
        id_number: str,
        name: str,
        gender: str,
        problem_description: str
):
    current_year = str(datetime.datetime.now().year)
    current_month = str(datetime.datetime.now().month).zfill(2)
    current_day = str(datetime.datetime.now().day).zfill(2)
    year_folder_path = os.path.join("D:\\robotlearn\\agent\\Customs_agent_assistant\\hsa-master\\test\\raw_data", current_year)
    year_month_folder_path = os.path.join(year_folder_path, f"{current_year}-{current_month}")
    if not os.path.exists(year_folder_path):
        os.mkdir(year_folder_path)
    if not os.path.exists(year_month_folder_path):
        os.mkdir(year_month_folder_path)
    file_name = f"{current_year}-{current_month}-{current_day}.json"
    file_path = os.path.join(year_month_folder_path, file_name)

    new_record = {
        "客户编号": id_number,
        "姓名": name,
        "性别": gender,
        "对话时间": {
            "年": str(datetime.datetime.now().year),
            "月": str(datetime.datetime.now().month).zfill(2),
            "日": str(datetime.datetime.now().day).zfill(2),
            "具体时间": datetime.datetime.now().strftime("%H:%M:%S")
        },
        "问题描述": problem_description
    }
    try:
        try:
            with open(file_path, 'r', encoding='utf-8') as file:
                data = json.load(file)
        except FileNotFoundError:
            data = []
        data.append(new_record)
        with open(file_path, 'w', encoding='utf-8') as file:
            json.dump(data, file, indent=4, ensure_ascii=False)
        print("记录添加成功。")
    except Exception as e:
        print(f"添加记录时出错: {e}")